package com.atguigu.service.impl;

import com.atguigu.service.CouponServiceV2;
import com.atguigu.utils.TaskDisposeUtils;
import jakarta.annotation.Resource;
import lombok.SneakyThrows;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CouponServiceImplV2 implements CouponServiceV2 {

    //下发优惠卷数量
    public static final Integer COUPON_COUNT = 50;
    @Resource
    private ThreadPoolTaskExecutor threadPool;

    @SneakyThrows
    @Override
    public void batchTaskActionV2() {
        //1.模拟要下发的50条优惠卷，上游系统给我们的下发优惠卷源头
        List<String> coupons = getCopons();

        long startTime = System.currentTimeMillis();

        //2.调用工具类批处理工具，这些优惠卷coupons，放入线程池threadPool了，执行业务逻辑
        TaskDisposeUtils.send(coupons,threadPool,TaskDisposeUtils::disposeTaskV4);

        long endTime = System.currentTimeMillis();
        System.out.println("-------costTime: "+(endTime-startTime)+"毫秒");

    }

    private List<String> getCopons() {
        List<String> coupons = new ArrayList<>(COUPON_COUNT);
        for (Integer i = 0; i < COUPON_COUNT; i++) {
            coupons.add("优惠卷--"+i);
        }
        return coupons;
    }
}
